//
//  UserRewardHeaderCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/1.
//

import UIKit
typealias UserRewardHeaderViewBlock = (NSInteger)->Void
class UserRewardHeaderView: UIView {
    var selectCallBack: UserRewardHeaderViewBlock?
    var bkgView: UIView!
    var collectionView: UICollectionView!
    var selectIndex: NSInteger = 0 {
        didSet {
            self.collectionView.reloadData()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bkgView = UIView(frame: CGRect.init(x: 14, y: 10, width: frame.size.width - 28, height: frame.size.height))
        bkgView.layer.masksToBounds = false
        //设置边框圆角
        bkgView.layer.cornerRadius = 8
        //设置阴影颜色
        bkgView.layer.shadowColor = UIColor.init(hex: 0xF4F4F9, alpha: 0.9).cgColor
        //设置透明度
        bkgView.layer.shadowOpacity = 1
        //设置阴影半径
        bkgView.layer.shadowRadius = 10
        //设置阴影偏移量
        bkgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.addSubview(bkgView)
        
        
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: bkgView.frame.size.width/3, height: bkgView.frame.size.height)
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: bkgView.frame.size.width, height: bkgView.frame.size.height), collectionViewLayout: layout)
        self.collectionView.register(UserRewardHeaderItemCell.self, forCellWithReuseIdentifier: "UserRewardHeaderItemCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.white
        bkgView.addSubview(self.collectionView)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.top.equalTo(10)
            make.height.greaterThanOrEqualTo(82)
            make.bottom.equalTo(-5).priority(.high)
        }
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension UserRewardHeaderView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UserRewardHeaderItemCell", for: indexPath) as! UserRewardHeaderItemCell
        var imgName = ""
        switch indexPath.row {
            case 0:
                imgName = "user_coupon"
                cell.titleLab.text = MMLocalizedString("user_coupon", comment: "优惠券")
            case 1:
                imgName = "user_gift"
                cell.titleLab.text = MMLocalizedString("user_gif_center", comment: "礼品中心")
            case 2:
                imgName = "user_point"
                cell.titleLab.text = MMLocalizedString("points", comment: "积分")
            default:
                ()
        }
        if indexPath.row == self.selectIndex {
            cell.imgView.image = UIImage.init(named: imgName + "_pre")
            cell.titleLab.textColor = Theme.mainColor
        }else{
            cell.imgView.image = UIImage.init(named: imgName)
            cell.titleLab.textColor = Theme.titleLabColor
        }
        return cell
    }
        
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectCallBack?(indexPath.row)
        self.selectIndex = indexPath.row
    }
}

